package com.flx.cloud.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.flx.cloud.sentinel.handler.DefaultBlockHandler;
import com.flx.cloud.sentinel.handler.DefaultFallbackHandler;
import com.flx.cloud.sentinel.service.SentinelService;
import com.flx.common.utils.result.ResultResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Fenglixiong
 * @Create 2022/4/22 18:50
 * @Description
 **/
@Api
@Slf4j
@RestController
@RequestMapping("/sentinel")
public class SentinelController {

    @Autowired
    private SentinelService sentinelService;

    /**
     * blockHandlerClass 指定限流处理类，不指定为当前类
     * blockHandler 指定限流处理方法
     */
    @GetMapping("/testHello")
    @SentinelResource(value = "testHello",
            blockHandlerClass = DefaultBlockHandler.class,blockHandler = "helloBlockHandler",
            fallbackClass = DefaultFallbackHandler.class,fallback = "helloFallBack")
    public ResultResponse testHello(@RequestParam String name){
        if(name.equals("error")){
            int a = 1/0;
        }
        return ResultResponse.success(sentinelService.testHello(name));
    }

    @GetMapping("/testBlockHandler")
    public ResultResponse testBlockHandler(@RequestParam String name){
        return ResultResponse.success(sentinelService.testBlockHandler(name));
    }

    @GetMapping("/testFallbackHandler")
    public ResultResponse testFallbackHandler(@RequestParam String name){
        return ResultResponse.success(sentinelService.testFallbackHandler(name));
    }

    @GetMapping("/testGetInfo")
    public ResultResponse testGetInfo(){
        return ResultResponse.success(sentinelService.testGetInfo());
    }

    @GetMapping("/testOther")
    public ResultResponse testOther(){
        return ResultResponse.success("ok");
    }

}
